﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace CopyRight
{
    class Removes
    {
        ///\*[\W|\w]*\*/\W*

        public static void RemoveCopyRight(string path)
        {

            RemoveCopyRight(path, @"/\*[\W|\w]*?\*/\s*", ".cs");
            RemoveCopyRight(path, @"<!--[\w|\W]*?-->\s*", ".xaml");
        }

        public static void RemoveCopyRight(string path, string regex, string filter)
        {
            List<string> ls = GetAllFiles(path, filter);
            foreach (string s in ls)
            {
                RemoveCopyRight(s, regex);
            }
        }
        public static void RemoveCopyRight(string fileName, string regex)
        {
            SaveStingToFile(fileName, Regex.Replace(ReadStringFromFile(fileName), regex, "", RegexOptions.IgnoreCase));
        }

        public static List<string> GetAllFiles(string path, string filter)
        {
            List<string> ls = new List<string>();

            if (Directory.Exists(path))
            {
                DirectoryInfo di = new DirectoryInfo(path);

                foreach (DirectoryInfo item in di.GetDirectories())
                {
                    ls.AddRange(GetAllFiles(item.FullName, filter));
                }

                foreach (FileInfo item in di.GetFiles())
                {
                    ls.AddRange(GetAllFiles(item.FullName, filter));
                }
            }
            else if (File.Exists(path))
            {
                ls.Add(path);
            }



            ls.RemoveAll((str) =>
            {
                FileInfo fi = new FileInfo(str);
                if (fi.Extension == filter) return false;
                return true;
            });
            return ls;
        }



        #region 读写文件

        #region 写文件
        /// <summary>
        /// 以UTF-8编码把字符串以覆盖的方式存入指定的文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="str">所要储存的内容</param>
        public static void SaveStingToFile(string fileName, string str)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8))
            {
                sw.Write(str);
                sw.Flush();
            }
        }
        /// <summary>
        /// 指定编码把字符串以覆盖的方式存入指定的文件
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <param name="str">所要储存的内容</param>
        public static void SaveStingToFile(string fileName, string str, Encoding encoding)
        {
            using (StreamWriter sw = new StreamWriter(fileName, false, encoding))
            {
                sw.Write(str);
                sw.Flush();
            }
        }

        #endregion
        #region 读文件
        /// <summary>
        /// 以UTF-8编码读取文件中的字符串
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>读取到的内容</returns>
        public static string ReadStringFromFile(string fileName)
        {
            return File.ReadAllText(fileName, Encoding.UTF8);
        }

        /// <summary>
        /// 指定编码读取文件中的字符串
        /// </summary>
        /// <param name="fileName">文件路径</param>
        /// <returns>读取到的内容</returns>
        public static string ReadStringFromFile(string fileName, Encoding encoding)
        {
            return File.ReadAllText(fileName, encoding);
        }

        #endregion

        #endregion
    }
}
